package com.rotsel.mh3udb.app.DataSource;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import android.widget.Toast;

import com.rotsel.mh3udb.app.Extra.Formatter;
import com.rotsel.mh3udb.app.ListModels.IdNameModel;
import com.rotsel.mh3udb.app.R;
import com.rotsel.mh3udb.app.models.ElementModel;
import com.rotsel.mh3udb.app.models.MainMenuModel;
import com.rotsel.mh3udb.app.models.WeaponModel;
import com.rotsel.mh3udb.app.models.WeaponTypeModel;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Roman on 17.05.2014.
 */
public class DataManager {

    private SQLiteDatabase database;
    public DataManager(Context context){
        DataManagerOpenHelper helper = new DataManagerOpenHelper(context);
        database = helper.getReadableDatabase();
    }

    public boolean updateDatabase(){
        /*try {

            // SET CONNECTIONSTRING
            Class.forName("net.sourceforge.jtds.jdbc.Driver").newInstance();
            String username = "esteet.ee";
            String password = "g$7iB9Qpg";
            Connection DbConn = DriverManager.getConnection("jdbc:jtds:sqlserver:web1.neonit.ee;user=" + username + ";password=" + password);

            Log.w("Connection","open");
            Statement stmt = DbConn.createStatement();
            ResultSet reset = stmt.executeQuery(" select * from users ");

            Toast.makeText(cont, reset.toString(),Toast.LENGTH_LONG );

            DbConn.close();

        } catch (Exception e)
        {
            Log.w("Error connection","" + e.getMessage());
        }*/

        return false;//TODO: connect to external database ang get updates
    }

    public ArrayList<MainMenuModel> getAllMenus(){
        Cursor cursor = database.rawQuery("SELECT [Id], [Name], [Key], [IsActive] FROM MainMenu", null);
        ArrayList<MainMenuModel> result = new ArrayList<MainMenuModel>();

        if (cursor != null && cursor.getCount() > 0){
            if (cursor.moveToFirst()){
                do {
                    int Id = cursor.getInt(0);
                    String name = cursor.getString(1);
                    String key = cursor.getString(2);
                    boolean isActive = cursor.getInt(3) > 0;

                    MainMenuModel model = new MainMenuModel(
                            Id,
                            name,
                            key,
                            isActive
                    );

                    result.add(model);
                } while (cursor.moveToNext());
            }

            if (!cursor.isClosed()) {
                cursor.close();
            }
        }
        return result;
    }

    public ArrayList<WeaponTypeModel> getAllWeaponTypes(){
        Cursor cursor = database.rawQuery("SELECT [Id], [Name], [ShortName] FROM WeaponTypes", null);
        ArrayList<WeaponTypeModel> result = new ArrayList<WeaponTypeModel>();

        if (cursor != null && cursor.getCount() > 0){
            result.ensureCapacity(cursor.getCount());
            if (cursor.moveToFirst()){
                do {
                    int Id = cursor.getInt(0);
                    String name = cursor.getString(1);
                    String shortName = cursor.getString(2);

                    WeaponTypeModel model = new WeaponTypeModel(
                            Id,
                            name,
                            shortName,
                            PicCol.getWpnTypePicByWpnTypeId(Id)
                    );

                    result.add(model);
                } while (cursor.moveToNext());
            }

            if (!cursor.isClosed()) {
                cursor.close();
            }
        }
        return result;
    }

    public WeaponTypeModel getWeaponTypeById(int id){
        Cursor cursor = database.rawQuery("SELECT [Id], [Name], [ShortName] FROM WeaponTypes WHERE [Id] = ?", new String[]{String.valueOf(id)});
        if (cursor == null || cursor.getCount() != 1){
            return null;
        }

        WeaponTypeModel model = null;
        if (cursor.moveToFirst()){
            int Id = cursor.getInt(0);
            String name = cursor.getString(1);
            String shortName = cursor.getString(2);

            model = new WeaponTypeModel(
                    Id,
                    name,
                    shortName,
                    PicCol.getWpnTypePicByWpnTypeId(Id)
            );
        }

        if (!cursor.isClosed()) {
            cursor.close();
        }

        return model;
    }

    public String getWeaponTypeNameById(int id){
        WeaponTypeModel model = getWeaponTypeById(id);
        if (model == null){
            return "";
        }
        return model.getName();
    }

    public String getWeaponTypeShortNameById(int id){
        WeaponTypeModel model = getWeaponTypeById(id);
        if (model == null){
            return "";
        }
        return model.getShortName();
    }

    public ArrayList<ElementModel> getAllElements(){
        Cursor cursor = database.rawQuery("SELECT [Id], [Name] FROM Elements", null);
        ArrayList<ElementModel> result = new ArrayList<ElementModel>(cursor.getCount());

        if (cursor != null && cursor.getCount() > 0){
            result.ensureCapacity(cursor.getCount());
            if (cursor.moveToFirst()){
                do {
                    int Id = cursor.getInt(0);
                    String name = cursor.getString(1);

                    ElementModel model = new ElementModel(
                            Id,
                            name,
                            PicCol.getElementIconById(Id)
                    );

                    result.add(model);
                } while (cursor.moveToNext());
            }

            if (!cursor.isClosed()) {
                cursor.close();
            }
        }
        return result;
    }

    public ElementModel getElementById(int id){
        Cursor cursor = database.rawQuery("SELECT [Id], [Name] FROM Elements WHERE [Id] = ?", new String[]{String.valueOf(id)});
        if (cursor == null || cursor.getCount() != 1){
            return null;
        }

        ElementModel model = null;
        if (cursor.moveToFirst()){
            int Id = cursor.getInt(0);
            String name = cursor.getString(1);

            model = new ElementModel(
                    Id,
                    name,
                    PicCol.getElementIconById(Id)
            );
        }

        if (!cursor.isClosed()) {
            cursor.close();
        }

        return model;
    }

    public ArrayList<IdNameModel> getListWeaponsByWpnTypeId(int weaponTypeId){
        Cursor cursor = database.rawQuery("SELECT [Id], [Name] FROM Weapons WHERE WeaponTypeId = ?", new String[]{String.valueOf(weaponTypeId)});
        ArrayList<IdNameModel> result = new ArrayList<IdNameModel>();

        if (cursor != null && cursor.getCount() > 0){
            if (cursor.moveToFirst()){
                result.ensureCapacity(cursor.getCount());
                do {
                    int Id = cursor.getInt(0);
                    String name = cursor.getString(1);

                    IdNameModel model = new IdNameModel(
                            Id,
                            name
                    );

                    result.add(model);
                } while (cursor.moveToNext());
            }

            if (!cursor.isClosed()) {
                cursor.close();
            }
        }
        return result;
    }

    public ArrayList<IdNameModel> getListWeapons(String[] ids){
        Cursor cursor = database.rawQuery("SELECT [Id], [Name] FROM Weapons IN (?)", Formatter.joinForSqlIn(ids));
        ArrayList<IdNameModel> result = new ArrayList<IdNameModel>();

        if (cursor != null && cursor.getCount() > 0){
            if (cursor.moveToFirst()){
                result.ensureCapacity(cursor.getCount());
                do {
                    int Id = cursor.getInt(0);
                    String name = cursor.getString(1);

                    IdNameModel model = new IdNameModel(
                            Id,
                            name
                    );

                    result.add(model);
                } while (cursor.moveToNext());
            }

            if (!cursor.isClosed()) {
                cursor.close();
            }
        }
        return result;
    }

    public WeaponModel getWeaponById(int id){
        Cursor cursor = database.rawQuery("SELECT [Id]," +
                "[Name]," +
                "[WeaponTypeId]," +
                "[WeaponTreeId]," +
                "[Rarity]," +
                "[WeaponUpgradeId]," +
                "[RawDamage]," +
                "[BuyingPrice]," +
                "[SellingPrice]," +
                "[Affinity]," +
                "[Defense]," +
                "[SlotCount]," +
                "[Description]," +
                "[Sharpness]," +
                "[SharpnessUp]," +
                "[ElementId]," +
                "[ElementDamage]," +
                "[AwakenElementId]," +
                "[AwakenElementDamage]," +
                "[SecondElementId]," +
                "[SecondElementDamage]," +
                "[SecondAwakenElementId]," +
                "[SecondAwakenElementDamage]," +
                "[ShellingId]," +
                "[ShellingLvl]," +
                "[PhialId]," +
                "[ReloadId]," +
                "[RecoilId]," +
                "[DeviationId]," +
                "[BarrelDamage] FROM Weapons WHERE [Id] = ?", new String[]{String.valueOf(id)});
        if (cursor == null || cursor.getCount() == 0){
            return null;
        }

        WeaponModel model = null;
        if (cursor.moveToFirst()){
            int weaponId = cursor.getInt(0);
            String name = cursor.getString(1);
            int weaponTypeId = cursor.getInt(2);
            int weaponTreeId = cursor.getInt(3);
            int rarity = cursor.getInt(4);
            int weaponUpgradeId = cursor.getInt(5);
            int rawDamage = cursor.getInt(6);
            int buyingPrice = cursor.getInt(7);
            int sellingPrice = cursor.getInt(8);
            int affinity = cursor.getInt(9);
            int defense = cursor.getInt(10);
            int slotCount = cursor.getInt(11);
            String description = cursor.getString(12);
            String sharpness = cursor.getString(13);
            String sharpnessUp = cursor.getString(14);
            int elementId = cursor.getInt(15);
            int elementDamage = cursor.getInt(16);
            int awakenElementId = cursor.getInt(17);
            int awakenElementDamage = cursor.getInt(18);
            int secondElementId = cursor.getInt(19);
            int secondElementDamage = cursor.getInt(20);
            int secondAwakenElementId = cursor.getInt(21);
            int secondAwakenElementDamage = cursor.getInt(22);
            int shellingId = cursor.getInt(23);
            int shellingLvl = cursor.getInt(24);
            int phialId = cursor.getInt(25);
            int reloadId = cursor.getInt(26);
            int recoilId = cursor.getInt(27);
            int deviationId = cursor.getInt(28);
            int barrelDamage = cursor.getInt(29);

            model = new WeaponModel(weaponId,
                    name,
                    weaponTypeId,
                    weaponTreeId,
                    rarity,
                    weaponUpgradeId,
                    rawDamage,
                    buyingPrice,
                    sellingPrice,
                    affinity,
                    defense,
                    slotCount,
                    description,
                    sharpness,
                    sharpnessUp,
                    elementId,
                    elementDamage,
                    awakenElementId,
                    awakenElementDamage,
                    secondElementId,
                    secondElementDamage,
                    secondAwakenElementId,
                    secondAwakenElementDamage,
                    shellingId,
                    shellingLvl,
                    phialId,
                    reloadId,
                    recoilId,
                    deviationId,
                    barrelDamage);
        }

        if (!cursor.isClosed()) {
            cursor.close();
        }

        return model;
    }
}
